package com.just.fun.arithmetic;

/**
 * 前序、中序、后序遍历
 * 最简单的是递归算法
 * 非递归可以借助栈来实现
 */
public class TreeNodeTest2 {
    public static void main(String[] args) {
         TreeNode root=TreeNodeTest.buildTreeNode();
         System.out.println("先序");
         firstPrint(root);
         System.out.println("中序");
         midPrint(root);
         System.out.println("后序");
         endPrint(root);
    }

    public static void firstPrint(TreeNode root){
        System.out.println(root.val);
        if(root.left!=null){
            firstPrint(root.left);
        }
        if(root.right!=null){
            firstPrint(root.right);
        }
    }

    public static void midPrint(TreeNode root){
        if(root.left!=null){
            midPrint(root.left);
        }
        System.out.println(root.val);
        if(root.right!=null){
            midPrint(root.right);
        }
    }

    public static void endPrint(TreeNode root){
        if(root.left!=null){
            endPrint(root.left);
        }
        if(root.right!=null){
            endPrint(root.right);
        }
        System.out.println(root.val);
    }
}
